/*
 * Atomic Predicates for Transformers
 * 
 * Copyright (c) 2015 UNIVERSITY OF TEXAS AUSTIN. All rights reserved. Developed
 * by: HONGKUN YANG and SIMON S. LAM http://www.cs.utexas.edu/users/lam/NRL/
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * with the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimers.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimers in the documentation
 * and/or other materials provided with the distribution.
 * 
 * 3. Neither the name of the UNIVERSITY OF TEXAS AUSTIN nor the names of the
 * developers may be used to endorse or promote products derived from this
 * Software without specific prior written permission.
 * 
 * 4. Any report or paper describing results derived from using any part of this
 * Software must cite the following publication of the developers: Hongkun Yang
 * and Simon S. Lam, Scalable Verification of Networks With Packet Transformers
 * Using Atomic Predicates, IEEE/ACM Transactions on Networking, October 2017,
 * Volume 25, No. 5, pages 2900-2915 (first published as IEEE Early Access
 * Article, July 2017, Digital Object Identifier: 10.1109/TNET.2017.2720172).
 * 
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH
 * THE SOFTWARE.
 */
package transformer;

import java.util.*;

public class PerformanceEvaluation {
    
    ArrayList<Double> datas;
    int window_size;
    // windowed sum
    double sum;
    double square_sum;
    //
    double threshold;
    boolean stable;
    
    int warm_up;
    
    public PerformanceEvaluation(int w, double t)
    {
        datas = new ArrayList<Double> ();
        window_size = w;
        threshold = t;
        
        warm_up = 10;
        
        sum = 0;
        square_sum = 0;
        stable = false;
    }
    
    public double get_mean()
    {
        return sum / window_size;
    }
    
    public double get_var()
    {
        return 1/(window_size-1.0)*(square_sum - sum*sum/window_size);
    }
    
    public boolean is_stable()
    {
        return stable;
    }
    
    public void add_data(double data)
    {
        System.out.println("add " + data);
        datas.add(data);
        sum += data;
        square_sum += data * data;
        
        
        
        if(datas.size() > window_size)
        {
            double old_data = datas.get(datas.size()-1-window_size);
            sum -= old_data;
            square_sum -= old_data * old_data;
            
            System.out.println("mean:" + get_mean());
            
            //System.out.println(get_var());
            double ratio = window_size*window_size*get_var()/(sum*sum);
            
            System.err.println("var divided by mean square: " + ratio);
            
            //stable = check_stable(ratio);
            stable = check_stable();
        }
        
    }
    
    private boolean check_stable(double ratio)
    {
        if(ratio < threshold)
        {
            return true;
        }else
        {
            return false;
        }
    }
    
    private boolean check_stable()
    {
        if(datas.size() == warm_up + window_size)
        {
            return true;
        }
        
        return false;
    }

}
